import DefensiveFacility from "../../../facility/DefensiveFacility";
import { FacilityType } from "../../../info/GameEnum";
import { AiEnvironment } from "../AiGameAction";
import { DecisionNode } from "../DecisionTree";
import DecisionAttackEnemyBase from "./attack/DecisionAttackEnemyBase";
import DecisionAttactUpgrade from "./attack/DecisionAttactUpgrade";
import DecisionBuildAttackBase from "./attack/DecisionBuildAttackBase";

/**
 * 攻击
 * 主基地血量比例越高
 * 自身炮弹数量越充足
 */
export default class DecisionAttack extends DecisionNode {

    /**
     * threatValue : 威胁值
     * sheelScore : 炮弹数量的分数
     */
    environment: AiEnvironment & { sheelScore: number };

    constructor(environment: any) {
        super(environment);
        this.childDecisionNodes.push(new DecisionAttackEnemyBase(environment));
        this.childDecisionNodes.push(new DecisionAttactUpgrade(environment));
        this.childDecisionNodes.push(new DecisionBuildAttackBase(environment));
    }

    /**
     * 计算自身基地血量
     * 最高
     * @returns 
     */
    evaluate(): number {
        let score = 0;
        //自身基地血量越高，分数越高
        let selfHome = this.environment.player.facilityArr[FacilityType.主基地][0]
        let hpRate = selfHome.currentHp / selfHome.maxHp;
        score += Math.ceil(hpRate * 60);

        //威胁值不如我的
        let LosingToMeCount = 0;
        this.environment.enemyThreatValue.forEach(value => {
            value < this.environment.selfThreatValue && LosingToMeCount++;
        });
        score += LosingToMeCount * 30;

        if (this.environment.invisibilityAttackedFacility.find(item => item instanceof DefensiveFacility)
            || this.environment.invisibilityAttackedFacility.find(item => item instanceof DefensiveFacility)
        ) {
            score += 50;
        }

        return score;
    }


    execute(): number {
        //炮弹数量的分数
        let score = 0;
        score += Math.min(this.environment.player.howitzerCount * 20, 40);
        score += Math.min(this.environment.player.rocketCount * 30, 30);
        score += Math.min(this.environment.player.electricCount * 30, 40);
        score += Math.min(this.environment.player.badgerCount * 10, 10);
        score += Math.min(this.environment.player.rocketCount * 40, 40);
        score += Math.min(this.environment.player.intercontinentalMissileCount * 50, 60);
        score += Math.min(this.environment.player.nuclearBombCount * 100, 100);
        this.environment.sheelScore = score;
        return super.execute();
    }
}
